/*
 * ModuleTag.java
 *
 * Copyright 2012 the original author or authors(ninglong).
 *
 * Licensed under the org.geek.user.web, Version 1.0 (the "License");
 *
 */
package org.geek.organ.web;

import java.util.List;

import org.geek.common.entity.ColumnModel;
import org.geek.common.tag.TreeGridPanelTag;

/**
 * <p></p>
 * @author ninglong bhd2973@qq.com
 * @version V1.0
 */
public class OrganTag extends TreeGridPanelTag {

	private static final long serialVersionUID = 7991363340167717983L;

	private String module;
	private String treeTitle;
	private String treeIconCls;
	private String treeUrl;
	private String gridTitle;
	private String gridIconCls;
	private String gridUrl;
	private List<ColumnModel> columnModel;
	private String addUrl;
	private String delUrl;
	private String modUrl;
	
	@Override
	public void genernateWindow(StringBuffer buffer){
		buffer.append("	var win_"+this.getId(module)+" = new Ext.Window({															").append("\r\n");
		buffer.append("			resizable : false,																					").append("\r\n");
		buffer.append("			animateTarget : document.body,																		").append("\r\n");
		buffer.append("			closeAction: 'hide',																				").append("\r\n");
		buffer.append("			plain: true,																						").append("\r\n");
		buffer.append("			modal: true,																						").append("\r\n");
		buffer.append("			items : [{																							").append("\r\n");
		buffer.append("				xtype : 'form',																					").append("\r\n");
		buffer.append("				id : 'form_"+this.getId(module)+"',																").append("\r\n");
		buffer.append("				border : false,																					").append("\r\n");
		buffer.append("				defaults : {																					").append("\r\n");
		buffer.append("					width : 260																					").append("\r\n");
		buffer.append("				},																								").append("\r\n");
		buffer.append("				bodyStyle : 'padding:15 15 15 15',																").append("\r\n");
		buffer.append("				defaultType : 'textfield',																		").append("\r\n");
		buffer.append("				labelWidth : 80,																				").append("\r\n");
		buffer.append("				labelSeparator : '：',																			").append("\r\n");
		buffer.append("				items : [{																						").append("\r\n");
		buffer.append("					fieldLabel : '机构名',																		").append("\r\n");
		buffer.append("					allowBlank : false,																			").append("\r\n");
		buffer.append("					maxLength : 20,																				").append("\r\n");
		buffer.append("					maxLengthText : '机构名的最大长度为20个字符',														").append("\r\n");
		buffer.append("					blankText : '机构名不能为空,请输入!',																").append("\r\n");
		buffer.append("					id : '"+getId("organName")+"',																").append("\r\n");
		buffer.append("					name : 'organName'																			").append("\r\n");
		buffer.append("				},new Ext.ux.ComboBoxTree({																		").append("\r\n");
		buffer.append("					fieldLabel : '上级机构',																		").append("\r\n");
		buffer.append("					hiddenName  : 'parentName',																	").append("\r\n");
		buffer.append("					allowBlank : false,																			").append("\r\n");
		buffer.append("					blankText : '上级机构不能为空,请选择!',																").append("\r\n");
		buffer.append("					tree : new Ext.tree.TreePanel({																").append("\r\n");
		buffer.append("						rootVisible:false,																		").append("\r\n");
		buffer.append("						loader: new Ext.tree.TreeLoader({														").append("\r\n");
		buffer.append("							dataUrl:'"+this.treeUrl+"' ,														").append("\r\n");
		buffer.append("							clearOnLoad: true,																	").append("\r\n");
		buffer.append("							listeners: {																		").append("\r\n");
		buffer.append("								'beforeload': function(treeloader, node) {										").append("\r\n");
		buffer.append("									treeloader.baseParams = {													").append("\r\n");
		buffer.append("										id: node.id,															").append("\r\n");
		buffer.append("										expanded : true,														").append("\r\n");
		buffer.append("										method: 'POST'															").append("\r\n");
		buffer.append("									}																			").append("\r\n");
		buffer.append("								}																				").append("\r\n");
		buffer.append("							}																					").append("\r\n");
		buffer.append("						}), 																					").append("\r\n");
		buffer.append("					 	root : new Ext.tree.AsyncTreeNode({id:'0',text:'根结点'})									").append("\r\n");
		buffer.append("					 }), 																						").append("\r\n");
		buffer.append("					 selectNodeModel:'exceptRoot', 																").append("\r\n");
		buffer.append("					 listeners:{'afterchange':function(lnode,n,o){Ext.getCmp('"+getId("parentId")+"').setValue(n.id);alert(n.id);}}").append("\r\n");
		buffer.append("				})																								").append("\r\n");
		buffer.append("				,{																								").append("\r\n");
		buffer.append("					hidden : true,																				").append("\r\n");
		buffer.append("					id : '"+getId("organId")+"',																").append("\r\n");
		buffer.append("					value : 0,																					").append("\r\n");
		buffer.append("					name : 'id'																					").append("\r\n");
		buffer.append("				},{																								").append("\r\n");
		buffer.append("					hidden : true,																				").append("\r\n");
		buffer.append("					id : '"+getId("parentId")+"',																").append("\r\n");
		buffer.append("					name : 'parentId'																					").append("\r\n");
		buffer.append("				}]																								").append("\r\n");
		buffer.append("			}],																									").append("\r\n");
		buffer.append("			buttons : [{																						").append("\r\n");
		buffer.append("				text : '保存',																					").append("\r\n");
		buffer.append("				iconCls : 'save',																				").append("\r\n");
		buffer.append("				handler : save_"+getId(module)+"																").append("\r\n");
		buffer.append("			},{																									").append("\r\n");
		buffer.append("				text : '重置',																					").append("\r\n");
		buffer.append("				iconCls : 'refresh',																				").append("\r\n");
		buffer.append("				handler : function() {																			").append("\r\n");
		buffer.append("  				Ext.getCmp('form_"+this.getId(module)+"').getForm().reset();								").append("\r\n");
		buffer.append("				}																								").append("\r\n");
		buffer.append("			}]																									").append("\r\n");
		buffer.append("		});																										").append("\r\n");
		
		buffer.append("  win_"+this.getId(module)+".on('show',function(){															").append("\r\n");
		buffer.append("  	Ext.getCmp('"+getId("organName")+"').focus();															").append("\r\n");
		buffer.append("  });																										").append("\r\n");
		
		save(buffer);
	}
	
	private void save(StringBuffer buffer){
		buffer.append(" function save_"+getId(module)+"(){																			").append("\r\n");
		buffer.append("  	var form = Ext.getCmp('form_"+this.getId(module)+"').getForm();											").append("\r\n");											
		buffer.append("  	var organId = Ext.getCmp('"+getId("organId")+"').getValue();											").append("\r\n");
		buffer.append("		var url = organId==0?'"+this.addUrl+"':'"+this.modUrl+"';												").append("\r\n");
		buffer.append("		if (form.isValid()) {																					").append("\r\n");
		buffer.append("			form.submit({																						").append("\r\n");
		buffer.append("				url : url,																						").append("\r\n");
		buffer.append("				waitTitle : '提示',																				").append("\r\n");
		buffer.append("				method : 'POST',																				").append("\r\n");
		buffer.append("				waitMsg : '正在提交数据,请稍候.....',																	").append("\r\n");
		buffer.append("				success : function(form, action) {																").append("\r\n");
		buffer.append("					Ext.Msg.alert('提示',action.result.msg, function() {											").append("\r\n");
		buffer.append("						"+this.getId(module)+"_store.reload();													").append("\r\n");
		buffer.append("						win_"+this.getId(module)+".hide();														").append("\r\n");
		buffer.append("						Ext.getCmp('"+this.getId(module)+"_leftTree').root.reload();							").append("\r\n");
		buffer.append("						Ext.getCmp('"+this.getId(module)+"_leftTree').expandAll();								").append("\r\n");
		buffer.append("					});																							").append("\r\n");
		buffer.append("				},																								").append("\r\n");
		buffer.append("				failure : function(form, action) {																").append("\r\n");
		buffer.append("					Ext.Msg.alert('提示',action.result.msg, function() {											").append("\r\n");
		buffer.append("  					Ext.getCmp('"+getId("moduleName")+"').focus();											").append("\r\n");
		buffer.append("						form.reset();																			").append("\r\n");
		buffer.append("					});																							").append("\r\n");
		buffer.append("				}																								").append("\r\n");
		buffer.append("			});																									").append("\r\n");
		buffer.append("		}																										").append("\r\n");
		buffer.append(" }																											").append("\r\n");
	}
	
	public String getModule() {
		return module;
	}
	public void setModule(String module) {
		super.setModule(module);
		this.module = module;
	}
	public String getTreeTitle() {
		return treeTitle;
	}
	public void setTreeTitle(String treeTitle) {
		super.setTreeTitle(treeTitle);
		this.treeTitle = treeTitle;
	}
	public String getTreeIconCls() {
		return treeIconCls;
	}
	public void setTreeIconCls(String treeIconCls) {
		super.setTreeIconCls(treeIconCls);
		this.treeIconCls = treeIconCls;
	}
	public String getTreeUrl() {
		return treeUrl;
	}
	public void setTreeUrl(String treeUrl) {
		super.setTreeUrl(treeUrl);
		this.treeUrl = treeUrl;
	}
	public String getGridTitle() {
		return gridTitle;
	}
	public void setGridTitle(String gridTitle) {
		super.setGridTitle(gridTitle);
		this.gridTitle = gridTitle;
	}
	public String getGridIconCls() {
		return gridIconCls;
	}
	public void setGridIconCls(String gridIconCls) {
		super.setGridIconCls(gridIconCls);
		this.gridIconCls = gridIconCls;
	}
	public String getGridUrl() {
		return gridUrl;
	}
	public void setGridUrl(String gridUrl) {
		super.setGridUrl(gridUrl);
		this.gridUrl = gridUrl;
	}
	public List<ColumnModel> getColumnModel() {
		return columnModel;
	}
	public void setColumnModel(List<ColumnModel> columnModel) {
		super.setColumnModel(columnModel);
		this.columnModel = columnModel;
	}
	public String getModUrl() {
		return modUrl;
	}
	public void setModUrl(String modUrl) {
		this.modUrl = modUrl;
	}

	public String getAddUrl() {
		return addUrl;
	}

	public void setAddUrl(String addUrl) {
		this.addUrl = addUrl;
	}

	public String getDelUrl() {
		return delUrl;
	}

	public void setDelUrl(String delUrl) {
		super.setDelUrl(delUrl);
		this.delUrl = delUrl;
	}
}
